﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;

namespace Wicresoft.RDW.Manage
{
    public partial class RoleList : BasePage_BackEnd
    {
        protected void btnShowOnClick(object sender, EventArgs e)
        {
            Response.Redirect("/Manage/RoleView.aspx?PageStatus=new");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                roleListData();
            }
        }
        public void roleListData()
        {
            var sysLog = from p in RolePresent.GetList()
                         where !p.SiteManager.Any() && !p.LabManager.Any()
                         select p;

            var list = sysLog.ToList();
            list.Add(new Model.Role() { RoleId = 0, Name = "Site Manager" });

            
            //list.AddRange(siteManger);

            rptRole.DataSource = list;
            rptRole.DataBind();
        }

        protected void GvRoleList_RowDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType== ListItemType.AlternatingItem)
            {
                Model.Role role = e.Item.DataItem as Model.Role;
                if (role != null && role.RoleId == 0)
                {
                    Literal litStyle = e.Item.FindControl("litStyle") as Literal;
                    Literal litEdit = e.Item.FindControl("litEdit") as Literal;
                    PlaceHolder phSiteManager = e.Item.FindControl("phSiteManager") as PlaceHolder;
                    Repeater rptSiteManager = e.Item.FindControl("rptSiteManager") as Repeater;

                    litStyle.Text = "style='cursor:pointer;' id='trSiteManager'";
                    litEdit.Text = "";
                    phSiteManager.Visible = true;

                    var siteManger = from p in RolePresent.GetList()
                             where p.SiteManager.Any()
                             select p;

                    rptSiteManager.DataSource = siteManger;
                    rptSiteManager.DataBind();
                }
            }
        }
        protected void rptSiteManager_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.Role role = e.Item.DataItem as Model.Role;
                if (role != null && role.RoleId > 0)
                {
                    PlaceHolder phLabManager = e.Item.FindControl("phLabManager") as PlaceHolder;
                    Repeater rptLabManager = e.Item.FindControl("rptLabManager") as Repeater;

                    phLabManager.Visible = true;
                    Model.SiteManager sm = role.SiteManager.First();
                    if (sm != null)
                    {
                        var query= from l in LabPresent.GetList()
                                   join lm in LabManagerPresent.GetList() on l.LabId equals lm.LabId
                                   join r in RolePresent.GetList() on lm.RoleId equals r.RoleId
                                   where l.LocationId == sm.LocationId
                                   select r;

                        rptLabManager.DataSource = query;
                        rptLabManager.DataBind();
                    }
                }
            }
        }
    }
}